use super::{
    err, err_value, throw_err, Base, Client, Collect, ECode, Lock, Resp, SvCache, VBase, VMap,
    Value,
};

pub struct Common;

impl Base for Common {}

impl Common {
    pub async fn do_cmd(
        &self,
        model: &str,
        para: &Value,
        cmd: &str,
        client: &Client,
        cache: SvCache,
        lock: Lock,
    ) -> Resp {
        let data = para.ckobj()?;
        let line = data.ck_str("line")?;

        let collect = Collect::new(client, cache, line);

        match cmd {
            "adduser" | "rmuser" | "getuser" => match model {
                "auth" => collect.do_sub_cmd(model, cmd, para, "user", lock).await,
                _ => throw_err!(ECode::NoServe, format!("no {cmd} serve")),
            },
            _ => {
                let count = match model {
                    "auth" => data.k_bool("count").map(|_| vec!["user".to_string()]),
                    _ => None,
                };

                collect.do_cmd(model, cmd, para, lock, count).await
            }
        }
    }
}
